package com.thales.sentinel.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 许可证信息模型
 * 对应加密狗只读内存中存储的JSON数据结构
 */
public class LicenseInfo {
    
    @JsonProperty("expirationDate")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
    private LocalDateTime expirationDate;
    
    @JsonProperty("restrictions")
    private Restrictions restrictions;
    
    @JsonProperty("licenseAgreement")
    private LicenseAgreement licenseAgreement;
    
    public LicenseInfo() {}
    
    public LocalDateTime getExpirationDate() {
        return expirationDate;
    }
    
    public void setExpirationDate(LocalDateTime expirationDate) {
        this.expirationDate = expirationDate;
    }
    
    public Restrictions getRestrictions() {
        return restrictions;
    }
    
    public void setRestrictions(Restrictions restrictions) {
        this.restrictions = restrictions;
    }
    
    public LicenseAgreement getLicenseAgreement() {
        return licenseAgreement;
    }
    
    public void setLicenseAgreement(LicenseAgreement licenseAgreement) {
        this.licenseAgreement = licenseAgreement;
    }
    
    /**
     * 检查许可证是否已过期
     * @return true如果已过期，false如果未过期
     */
    public boolean isExpired() {
        if (expirationDate == null) {
            return false; // 如果没有设置过期时间，认为未过期
        }
        return LocalDateTime.now().isAfter(expirationDate);
    }
    
    /**
     * 获取最大用户数限制
     * @return 最大用户数，如果没有限制返回-1
     */
    public int getMaxUsers() {
        if (restrictions != null) {
            return restrictions.getMaxUsers();
        }
        return -1; // 无限制
    }
    
    /**
     * 检查是否启用了指定功能
     * @param feature 功能名称
     * @return true如果功能启用，false如果功能未启用
     */
    public boolean isFeatureEnabled(String feature) {
        if (restrictions != null && restrictions.getFeatureSet() != null) {
            return restrictions.getFeatureSet().contains(feature);
        }
        return false;
    }
    
    /**
     * 限制信息内部类
     */
    public static class Restrictions {
        @JsonProperty("maxUsers")
        private int maxUsers;
        
        @JsonProperty("featureSet")
        private List<String> featureSet;
        
        public Restrictions() {}
        
        public int getMaxUsers() {
            return maxUsers;
        }
        
        public void setMaxUsers(int maxUsers) {
            this.maxUsers = maxUsers;
        }
        
        public List<String> getFeatureSet() {
            return featureSet;
        }
        
        public void setFeatureSet(List<String> featureSet) {
            this.featureSet = featureSet;
        }
    }
    
    /**
     * 许可协议信息内部类
     */
    public static class LicenseAgreement {
        @JsonProperty("version")
        private String version;
        
        @JsonProperty("text")
        private String text;
        
        public LicenseAgreement() {}
        
        public String getVersion() {
            return version;
        }
        
        public void setVersion(String version) {
            this.version = version;
        }
        
        public String getText() {
            return text;
        }
        
        public void setText(String text) {
            this.text = text;
        }
    }
}